package com.hyyt.shopjsp.coinrules.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.coinrules.pojo.BasicCoinRules;
import com.hyyt.shopjsp.coinrules.service.IBasicCoinRulesService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘超 on 2017/7/11.
 */

@Controller
@RequestMapping("/back/coinRules")
public class BasicCoinRulesController extends BaseController{

    private Logger logger = Logger.getLogger(BasicCoinRulesController.class);
    private String basePath = "/back/coinRules/";

    @Resource
    private IBasicCoinRulesService basicCoinRulesService;

    /**
     * 进入金币策略
     *
     * @return 进入指定页面
     */
    @RequestMapping("gotoCoinRulesPage")
    public String gotoCoinRulesPage(){
        return basePath+"coinRulesList";
    }

    /**
     * 查询所有金币策略
     *
     * @param page
     * @param response
     */
    @RequestMapping(value = "listCoinRules", method = RequestMethod.POST)
    @ResponseBody
    public void listCoinRules(Page page,String name ,String typeName, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(name)) {//是否检索名称
            param.put("name", name.trim());
        }
        if (Utils.objectIsNotEmpty(typeName)) {//是否检索类型名称
            param.put("typeName", typeName.trim());
        }
        param.put("sortColumns", "coinRulesId desc");
        int totalRecordCount = basicCoinRulesService.countBasicCoinRules(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<BasicCoinRules> list = basicCoinRulesService.queryBasicCoinRules(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 根据金币策略id获得该对象
     *
     * @param coinRulesId
     */
    @RequestMapping(value = "getCoinRules", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getCoinRules(Integer coinRulesId) {
        if (Utils.objectIsNotEmpty(coinRulesId)) {
            BasicCoinRules basicCoinRules = basicCoinRulesService.loadBasicCoinRules(coinRulesId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(basicCoinRules));
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或者修改金币策略
     *
     * @param basicCoinRules
     * @return
     */
    @RequestMapping(value = "saveOrUpdateCoinRules", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateCoinRules(BasicCoinRules basicCoinRules) {
        BasicUsers basicUsers = getSessionUsers();
        if (Utils.objectIsNotEmpty(basicCoinRules)) {
            if (Utils.objectIsEmpty(basicCoinRules.getCoinRulesId())) {
                Timestamp createTime = new Timestamp(System.currentTimeMillis());
                basicCoinRules.setUserId(basicUsers.getUsersId());
                basicCoinRules.setUserName(basicUsers.getUserName());
                basicCoinRules.setCreateTime(createTime);
                basicCoinRulesService.saveBasicCoinRules(basicCoinRules);
            } else {
                Timestamp updateTime = new Timestamp(System.currentTimeMillis());
                basicCoinRules.setUserId(basicUsers.getUsersId());
                basicCoinRules.setUserName(basicUsers.getUserName());
                basicCoinRules.setUpdateTime(updateTime);
                basicCoinRulesService.updateBasicCoinRules(basicCoinRules);
            }
        }
        return ResultMsg.create(true);
    }

    /**
     * 根据传入的金币策略id删除
     *
     * @param coinRulesId
     */
    @RequestMapping(value = "deleteById", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteById(String coinRulesId) {
        if (Utils.objectIsNotEmpty(coinRulesId)) {
            basicCoinRulesService.deleteBasicCoinRules(Integer.parseInt(coinRulesId));
        }
        return ResultMsg.create(true);
    }

}
